<?php
include_once('common/header.php');
if($AppUI->id <= 0 || (isset($_SESSION['education']) && (($_SESSION['education']&1<<2) == 0))) {
	$this->_redirect(HOST_BACKEND);
}
$intEcoursId		= (int)$objRequest->getParam('pecoursid', 0);
$intCategoryId		= (int)$objRequest->getParam('pcategoryid', 0);
$intProvince		= (int)$objRequest->getParam('pprovince', 0);
$intEducateId		= (int)$objRequest->getParam('peducateid', 0);
$strName			= $objRequest->getParam('pname', '');
$strStart			= $objRequest->getParam('pstart', null);
$strPrice			= $objRequest->getParam('pprice', '');
$intUnit			= $objRequest->getParam('punit', 0);
$strShort			= $objRequest->getParam('pshort', '');
$strContent			= $objRequest->getParam('pcontent', '');
$intAdv				= $objRequest->getParam('padv', 0);
$strTag				= $objRequest->getParam('ptag', '');
$strFeature			= $objRequest->getParam('ptbfeature','');
$arrErr				= null;
//data
$strName			= Ecore_Function::stripTags($strName,-2);
$strShort			= Ecore_Function::stripTags($strShort,-2);
$strContent			= Ecore_Function::stripTags($strContent,-1);
$strTag				= Ecore_Function::stripTags($strTag,-2);
//validate
if($intCategoryId < 1) array_push($arrErr, $arrLocale['category_required']);
if($intProvince < 1) array_push($arrErr, $arrLocale['province_required']);
if($intEducateId < 1) array_push($arrErr, $arrLocale['educate_required']);
if($strName == '') array_push($arrErr, $arrLocale['name_required']);
elseif($strName != '' && strlen($strName) < VL_NAME_MINLENGTH) array_push($arrErr, $arrLocale['name_minlength']);
if($strShort != '' && strlen($strShort) < VL_SHORT_MINLENGTH) array_push($arrErr, $arrLocale['short_minlength']);
elseif($strShort != '' && strlen($strShort) > VL_SHORT_MAXLENGTH) array_push($arrErr, $arrLocale['short_maxlength']);
if($strContent == '') array_push($arrErr, $arrLocale['content_required']);
elseif($strContent != '' && strlen($strContent) < VL_CONTENT_MINLENGTH) array_push($arrErr, $arrLocale['content_minlength']);
//update
if(!isset($arrErr)) {
	if(isset($_SESSION['ECOURSE_POST'])) unset($_SESSION['ECOURSE_POST']);
	if(isset($_SESSION['ECOURSE_ERROR'])) unset($_SESSION['ECOURSE_ERROR']);	
	$strShort		= $strShort != '' ? $strShort : Ecore_Function::wordCut($strContent); 			
	$strStart		= (isset($strStart) && $strStart != '') ? Ecore_Function::convertStrToDate($strStart) : null;
	$arrPrice		= explode('-',$strPrice);
	$intPriceS		= 0;
	$intPriceE		= 0;
	if(count($arrPrice)>1){
		$intPriceS	= (int)str_replace(array('.',','),'',trim($arrPrice[0]));
		$intPriceE	= (int)str_replace(array('.',','),'',trim($arrPrice[1]));
	} else {
		$intPriceS	= (int)str_replace(array('.',','),'',$strPrice);
	}
	$objEcourse 	= new Ecourse();
	$intError		= $objEcourse->update($AppUI->id, $intEcoursId, $intEducateId, $strName, $strStart, 
										  $intPriceS, $intPriceE, $intUnit, $strShort, $strContent, 
										  $strTag, $intAdv, $strFeature);
	if($intError == 0) Ecore_Function::setMsg($arrMessage['ecourse_edit']);
	else array_push($arrErr, $arrError['noperm_ecourse_edit']);
}
if($intError > 0) {
	$_SESSION['ECOURSE_POST']	= $objRequest->getParams();
	$_SESSION['ECOURSE_ERROR']	= $arrErr;
	$this->_redirect(HOST_BACKEND . 'ecourse/edit?ecoursid='.$intEcoursId);
}
$this->_redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND . 'ecourse/index');